//
//  PhotoAlbumViewController.m
//  HomePage
//
//  Created by dlios on 15-1-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PhotoAlbumViewController.h"
#import "AFNetworking.h"
#import "PhotoAlbumAfter.h"
#import "PhotoAlbumCell.h"
#import "AudioPlayer.h"
#import <AudioStreamer.h>
#import "PlayMusicViewController.h"
#import <SVProgressHUD.h>

@interface PhotoAlbumViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)NSDictionary *dicA;


@end

@implementation PhotoAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arr = [NSMutableArray array];
    [self request];
    [self CreateTableView];
    
  
}
- (void)request
{
    //NSLog(@"kkkkkkk%@",self.number);
   // NSLog(@"llllllll%@",self.numberTwo);
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
            NSString *NumTwo = [formatter stringFromNumber:self.numberTwo];
        NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15",NumTwo];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [SVProgressHUD show];
        [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            self.dicA = responseObject;
            [SVProgressHUD dismiss];
            NSDictionary *dic = [self.dicA objectForKey:@"tracks"];
            NSMutableArray *arry = [dic objectForKey:@"list"];
            for (NSDictionary *dicList in arry) {
                HotThird *pAA = [[HotThird alloc] init];
                [pAA setValuesForKeysWithDictionary:dicList];
                [self.arr addObject:pAA];
            }
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          //  NSLog(@"error: %@", error);
            [SVProgressHUD dismiss];
        }];

    
    
}

- (void)CreateTableView
{
    //self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [ self.tableView reloadData];
    [self.view addSubview: self.tableView];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[AudioPlayer ShowInstance].streamer stop];
    HotThird *pAA = [self.arr objectAtIndex:indexPath.row];
    PlayMusicViewController *musicVc = [[PlayMusicViewController alloc] init];
    musicVc.Titlestr = pAA.title;
    musicVc.playArray = self.arr;
    musicVc.index = indexPath.row;
    musicVc.mp3Path = pAA.playUrl64;
    musicVc.imagePath = pAA.coverLarge;
    musicVc.musicPlay = pAA;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:musicVc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 5.68;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"hot";
    PhotoAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
           if (cell == nil) {
            cell = [[PhotoAlbumCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        PhotoAlbumAfter *pAA = [self.arr objectAtIndex:indexPath.row];
        cell.pAA = pAA;
    
    return cell;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
